﻿using System;
using System.Data;
using System.IO;
using System.Xml;
namespace HGZP.PLL
{
    /// <summary>
    /// XMLAnalyse 的摘要说明。
    ///作用：XML 解析 
    ///作者：郑文亮 裴建 刘鹏
    ///编写日期：2007-03-06
    /// 
    /// 作者：郑文亮
    /// 作用: 增加读写xml文件的2个小函数
    /// 
    /// </summary>
    public partial class XMLAnalyse
    {
        /// <summary>
        /// 根据xml和相应的节点 读出对应的值
        /// <param name="xmlString">xml字符串</param>
        /// <param name="key">节点的名称</param>
        /// <returns></returns>
        /// </summary>
        static public string GetValueByKey(string xmlString, string key)
        {
            string stringArray = string.Empty;
            if (xmlString.Trim() == string.Empty) { return stringArray; }
            //
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xmlString);

                XmlNodeList nodeList = xmlDoc.SelectNodes(@"/NewDataSet/" + key.Trim());
                foreach (XmlNode node in nodeList)
                {
                    if (node.Name.Trim() == key.Trim())
                        //stringArray[0] = "true";
                    stringArray = node.InnerText.Trim();
                }
            }
            catch (Exception ee)
            {
                Public.globalErrorString = ee.Message;
                stringArray = "Error";
                //stringArray = ee.Message.ToString();
            }
            //
            return stringArray;
        }
        /// <summary>
        /// 根据xml字符串形成Dataset
        /// </summary>
        /// <param name="xmlString">xml字符串参数</param>
        /// <returns></returns>
        static public DataSet GetDatasetByXMLString(string xmlString)
        {


            DataSet myDataSet = new DataSet();
            StringReader rd = new StringReader(xmlString);
            try
            {
                myDataSet.ReadXml((TextReader)rd);
            }
            catch (Exception ee)
            {
                Public.globalErrorString = ee.Message.ToString().Trim();
                return null;
            }
            //
            return myDataSet;

        }
        //写xmlPath是文件路径+文件名，AppKey是 Key Name，AppValue是Value
        static public void SetXmlFileValue(string xmlPath, string AppKey, string AppValue)
        {
            FileInfo myFileInfo = new FileInfo(xmlPath);
            myFileInfo.IsReadOnly = false;

            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(xmlPath);
            XmlNode xNode;
            XmlElement xElem1;
            XmlElement xElem2;

            xNode = xDoc.SelectSingleNode("//appSettings");

            xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
            if (xElem1 != null)
            {
                xElem1.SetAttribute("value", AppValue);
            }
            else
            {
                xElem2 = xDoc.CreateElement("add");
                xElem2.SetAttribute("key", AppKey);
                xElem2.SetAttribute("value", AppValue);
                xNode.AppendChild(xElem2);
            }
            xDoc.Save(xmlPath);
        }
        //读xmlPath是文件路径+文件名，AppKey是 Key Name，AppValue是Value
        static　public void GetXmlFileValue(string xmlPath, string AppKey, ref string AppValue)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(xmlPath);
            XmlNode xNode;
            XmlElement xElem1;

            xNode = xDoc.SelectSingleNode("//appSettings");

            xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
            if (xElem1 != null)
            {
                AppValue = xElem1.GetAttribute("value");
            }
            else
            {
                //    MessageBox.Show ("There is not any information!");
            }

        }
   

    }
}

